//
//  JXWSegmentViewController.m
//  JXWShopping4IPhone
//
//  Created by zheng ji on 14-1-19.
//  Copyright (c) 2014年 jxw. All rights reserved.
//

#import "JXWSegmentViewController.h"
#import "Constants.h"
#import "ProdListController.h"
#import "MobClick.h"
#define JXW_TESE 397//特色风格ID
#define JXW_LINGSHI 398//零食特产ID
#define JXW_CHANGFANG 399//厨房食材ID
#define JXW_ZIBU 396//滋补调理ID
@interface JXWSegmentViewController ()
{
    IBOutlet UISegmentedControl *segmentedControl;
    ProdListController* prodListController;
     NSUserDefaults *myDefaults;
   // NSMutableArray* productViewArray;//产品controller列表
    //NSMutableArray* viewShowArray;//存储已经显示的segmentcontroll 序号
}
@end

@implementation JXWSegmentViewController
- (IBAction)segmentControlValueChanged:(UISegmentedControl*)sender {
    [self showProductView:sender.selectedSegmentIndex];
    
    
    [MobClick beginEvent:@"segmentChange" primarykey:@"categoryList" attributes:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%d",sender.selectedSegmentIndex] forKey:@"selectedSegmentIndex" ]];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)showProductView:(NSUInteger)selectedSegmentIndex{

    if(  selectedSegmentIndex==0){
        [prodListController addParams:[NSString stringWithFormat:@"%d",JXW_TESE] forKey:@"materialid"];
    }else if(  selectedSegmentIndex==1){
        [prodListController addParams:[NSString stringWithFormat:@"%d",JXW_LINGSHI] forKey:@"materialid"];
        
    }else if(  selectedSegmentIndex==2){
        [prodListController addParams:[NSString stringWithFormat:@"%d",JXW_CHANGFANG] forKey:@"materialid"];
        
    }else if(  selectedSegmentIndex==3){
        [prodListController addParams:[NSString stringWithFormat:@"%d",JXW_ZIBU] forKey:@"materialid"];
        
    }
    
    
    
    [prodListController doSearch:@"reloadStart"];
    /*
    if(viewShowArray==nil) viewShowArray=[[NSMutableArray alloc]init];
  
    
    NSLog(@"count=%d",productViewArray.count);
      ProdListController* pc=(ProdListController*)[productViewArray objectAtIndex:productIndex ];
    NSString* strIndex=[NSString stringWithFormat:@"%d",productIndex];
    
    if([viewShowArray containsObject:strIndex]){
        [self.view bringSubviewToFront:pc.view];
    }else{
        [viewShowArray addObject:strIndex];
        [self.view addSubview:pc.view];
    }
    int index=0;
    for (ProdListController* controller in productViewArray) {
        if(index==productIndex){
            [controller.view setHidden:NO];
        }else{
            [controller.view setHidden:YES];
        }
        index++;
    }
    */
     
}
- (void)viewDidLoad
{
    [super viewDidLoad];
     myDefaults=[NSUserDefaults standardUserDefaults];
    [self createPromotionControllers];
    
    segmentedControl.selectedSegmentIndex=0;
	[self showProductView:0];
    
      [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(checkVersionTimer:) userInfo:nil repeats:NO];
}

-(void) checkVersionTimer:(NSTimer*)theTimer{
    [self checkVersion];
    [theTimer invalidate];
}



-(void)checkVersion{
    
    if([CommonConfigUtil checkVersion]){
        
        NSString *versionNumber=[myDefaults objectForKey:@"versionNumber"];
        NSString *appUrl=[myDefaults objectForKey:@"appUrl"];
        // NSString *appCommentUrl=[myDefaults objectForKey:@"appCommentUrl"];
        NSString *description=[myDefaults objectForKey:@"description"];
        
        if(nil==appUrl)appUrl=@"";
        if(nil==versionNumber)versionNumber=@"";
        if(nil==description)description=@"";
        
        
        if(![@"" isEqualToString:appUrl] && nil!=versionNumber&& ![@"" isEqualToString:versionNumber]&& [versionNumber compare: [CommonConfigUtil getCurrVersion] ]==NSOrderedDescending ){
            
            
            NSString *strMessge;
            if(nil!=description&&![@"" isEqualToString:description]){
                strMessge=[NSString stringWithFormat:@"%@版本发布啦！%@",versionNumber,description];
            }else{
                strMessge=[NSString stringWithFormat:@"%@版本发布啦！是否需要升级？",versionNumber];
            }
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"版本升级" message:strMessge                                                           delegate:self  cancelButtonTitle:@"立即升级" otherButtonTitles: @"忽略",nil];
            [alert setTag:1];
            
            [alert show];
            
            alert=nil;
        }
        
    }
    
}




- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if([alertView tag]==1&&buttonIndex == 0) {
        
        NSString *appUrl=  [myDefaults objectForKey:@"appUrl"];
        if(nil!=appUrl && ![@"" isEqualToString:appUrl]) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appUrl]];
        }
        
    }
    
}


-(void)createPromotionControllers{
    
    UIStoryboard* storyBoard=[UIStoryboard storyboardWithName:@"JXWMain" bundle:nil];
    
    
    if(IsIOS7){
        prodListController=[storyBoard instantiateViewControllerWithIdentifier:@"ProdListController4IOS7"];//特色风格
    }else{
        prodListController=[storyBoard instantiateViewControllerWithIdentifier:@"ProdListController"];//特色风格
    }
    [prodListController addParams:[NSString stringWithFormat:@"%d",JXW_TESE] forKey:@"materialid"];
    
    prodListController.view.frame=self.view.frame;
    
    prodListController.view.backgroundColor=[UIColor clearColor];
    prodListController.jxwSegmentController=self;
    
     [self.view addSubview:prodListController.view];
    storyBoard=nil;
    /*
    productViewArray=[[NSMutableArray alloc] init];
    UIStoryboard* storyBoard=[UIStoryboard storyboardWithName:@"JXWMain" bundle:nil];
    
    ProdListController  *pc1=nil;
    if(IsIOS7){
     pc1=[storyBoard instantiateViewControllerWithIdentifier:@"ProdListController4IOS7"];//特色风格
    }else{
        pc1=[storyBoard instantiateViewControllerWithIdentifier:@"ProdListController"];//特色风格
    }
    [pc1 addParams:[NSString stringWithFormat:@"%d",JXW_TESE] forKey:@"materialid"];
    
    ProdListController  *pc2=nil;
    
    if(IsIOS7){
          pc2=[storyBoard instantiateViewControllerWithIdentifier:@"ProdListController4IOS7"];//零食特产
    }else{
       pc2=[storyBoard instantiateViewControllerWithIdentifier:@"ProdListController"];//零食特产
    }
    [pc2 addParams:[NSString stringWithFormat:@"%d",JXW_LINGSHI] forKey:@"materialid"];
      
    ProdListController  *pc3=nil;
    
    if(IsIOS7){
        pc3=[storyBoard instantiateViewControllerWithIdentifier:@"ProdListController4IOS7"];//厨房食材
    }else{
        pc3
        =[storyBoard instantiateViewControllerWithIdentifier:@"ProdListController"];//厨房食材
    }

    [pc3 addParams:[NSString stringWithFormat:@"%d",JXW_CHANGFANG] forKey:@"materialid"];
    
    ProdListController  *pc4=nil;
    
    if(IsIOS7){
        pc4=[storyBoard instantiateViewControllerWithIdentifier:@"ProdListController4IOS7"];//滋补调理
    }else{
        pc4
        =[storyBoard instantiateViewControllerWithIdentifier:@"ProdListController"];//滋补调理
    }
    
    [pc4 addParams:[NSString stringWithFormat:@"%d",JXW_ZIBU] forKey:@"materialid"];

    
    
    
    pc1.view.frame=self.view.frame;
    pc2.view.frame=self.view.frame;
    pc3.view.frame=self.view.frame;
    pc4.view.frame=self.view.frame;

    pc1.jxwSegmentController=self;
    pc2.jxwSegmentController=self;
    pc3.jxwSegmentController=self;
    pc4.jxwSegmentController=self;

 NSString* showAdv=[CommonConfigUtil getShowadv];
    [pc1 addParams:showAdv forKey:@"showadv"];
    [pc2 addParams:showAdv forKey:@"showadv"];
    [pc3 addParams:showAdv forKey:@"showadv"];
    [pc4 addParams:showAdv forKey:@"showadv"];
    
    
    [productViewArray addObject:pc1];
    [productViewArray addObject:pc2];
    [productViewArray addObject:pc3];
    [productViewArray addObject:pc4];
     */
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 

@end
